package com.shop.controller;

import com.shop.domain.Cart;
import com.shop.entity.Result;
import com.shop.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 新增购物车
     * @param cart
     * @return
     */
    @PostMapping("/add")
    public Result addCart(@RequestBody Cart cart) {
        cartService.add(cart);
        return new Result("添加成功");
    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/findList")
    public Result findList() {
        List<Cart> list = cartService.findList();
        return new Result("查询成功",list);
    }

    /**
     * 修改购物车
     *
     * @param cart
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Cart cart) {
        try {
            cartService.update(cart);
        } catch (Exception e) {
            return Result.fail("修改失败");
        }
        return  Result.ok("修改成功");
    }

    /**
     * 删除购物车
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids) {
        try {
            cartService.delete(ids);
        } catch (Exception e) {
            return Result.fail("删除失败");
        }
        return new Result<>("删除成功");
    }


}
